package cn.liutao.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.params.SetParams;

/**
 * @author liutao
 * @Description Redis工具类
 * @date 2023/10/30 23:19
 * @Version 1.0
 */
@Data
@Slf4j
public class RedisUtils {
    private static JedisPool jedisPool;
    private static final RedisUtils instance = new RedisUtils();
    private RedisUtils(){
    }

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(128);
        poolConfig.setMaxIdle(128);
        poolConfig.setMinIdle(16);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        jedisPool = new JedisPool(poolConfig, "localhost", 6379); // 请将localhost和6379替换为你的Redis服务器地址和端口号

    }

    /**
     * 从jedis连接池中获取获取jedis对象
     *
     * @return
     */
    private static Jedis getJedis() {
        return jedisPool.getResource();
    }


    /**
     * 获取RedisUtil实例
     *
     * @return
     */
    public static RedisUtils getInstance() {
        return instance;
    }

    public static<T> void set(String key,T value){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.set(key, JSONObject.toJSONString(value));
        }finally {
            if (jedis!=null) jedis.close();
        }
    }

    public static void set(String key,String value){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.set(key, value);
        }finally {
            if (jedis!=null) jedis.close();
        }
    }

    public static<T> void set(String key, T value, Long second){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.set(key,JSONObject.toJSONString(value),new SetParams().ex(second));
        }finally {
            if (jedis!=null) jedis.close();
        }
    }
    public static void set(String key, String value, Long second){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.set(key,value,new SetParams().ex(second));
        }finally {
            if (jedis!=null) jedis.close();
        }
    }

    public static String get(String key){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            return jedis.get(key);
        }finally {
            if (jedis!=null) jedis.close();
        }
    }
    public static<T> T get(String key,Class<T> tClass){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            String jsonObj = jedis.get(key);
            if (StrUtil.isNotEmpty(jsonObj)){
                return JSONObject.parseObject(jsonObj,tClass);
            }else {
                return null;
            }
        }finally {
            if (jedis!=null) jedis.close();
        }
    }

    public static void remove(String key){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            jedis.del(key);
        }finally {
            if (jedis!=null) jedis.close();
        }
    }

}
